﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VnResource.DataContract.Models;
using VnResource.DataAccess;

namespace VnResource.BusinessLogic
{
    public class HRMRepository
    {
        #region Properties
        private static HRMRepository _instance = null;
        public static HRMRepository Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new HRMRepository();
                }
                return _instance;
            }
        }


        private HRM4DevelopingEntities _HRMContext;
        private HRM4DevelopingEntities HRMContext
        {
            get
            {
                if (_HRMContext == null)
                {
                    _HRMContext = new HRM4DevelopingEntities();
                }
                return _HRMContext;
            }
            set
            {
                _HRMContext = value;
            }
        } 
        #endregion
        
        #region Method
        
        public List<ProfileViewModel> GetProfile()
        {
            var profiles = HRMContext.Hre_Profile.AsEnumerable().Select(pm => new ProfileViewModel()
            {
                CodeEmp = pm.CodeEmp,
                ID = pm.ID,
                Position = pm.Position,
                ProfileName = pm.ProfileName
            }).ToList();
            return profiles;
        }

        public List<ProfileViewModel> GetApproveProfile()
        {
            string leader = HrePosition.Leader.ToString();
            var profiles = HRMContext.Hre_Profile.AsEnumerable().Where(p=>p.Position == leader).Select(pm => new ProfileViewModel()
            {
                CodeEmp = pm.CodeEmp,
                ID = pm.ID,
                Position = pm.Position,
                ProfileName = pm.ProfileName
            }).ToList();
            return profiles;
        }

        public LeaveDayViewModel GetLeaveDay(Guid id)
        {
            var leaveDay = HRMContext.Att_LeaveDay.AsEnumerable().Where(p => p.ID == id).Select(pm => new LeaveDayViewModel()
            {
                DateEnd = pm.DateEnd,
                DateStart = pm.DateStart,
                ID = pm.ID,
                IsDelete = pm.IsDelete,
                LeaveDayType = pm.LeaveDayType,
                ProfileID = pm.ProfileID,
                TotalDurian = pm.TotalDurian,
                UserApproveID = pm.UserApproveID,
                UserApproveID2 = pm.UserApproveID2
            }).FirstOrDefault();
           
            if (leaveDay != null)
            {               
                var profile =  GetProfile().Where(p => p.ID == leaveDay.ProfileID).FirstOrDefault();
                leaveDay.HreProfile = profile;
            }
            return leaveDay;
        }

        public List<LeaveDayViewModel> GetLeaveDays()
        {
            var profiles = GetProfile();

            var leaveDays = HRMContext.Att_LeaveDay.AsEnumerable().Where(p => p.IsDelete != true).Select(pm => new LeaveDayViewModel()
            {
                DateEnd = pm.DateEnd,
                DateStart = pm.DateStart,
                ID = pm.ID,
                IsDelete = pm.IsDelete,
                LeaveDayType = pm.LeaveDayType,
                ProfileID = pm.ProfileID,
                TotalDurian = pm.TotalDurian,
                UserApproveID = pm.UserApproveID,
                UserApproveID2 = pm.UserApproveID2
            }).ToList();

            foreach (var item in leaveDays)
            {
                var profile = profiles.Where(p => p.ID == item.ProfileID).FirstOrDefault();
                if (profile != null)
                {
                    item.HreProfile = profile;
                }
            }

            return leaveDays;
        }

        public bool DeleteLeaveDay(LeaveDayViewModel model)
        {
            Guid id = model.ID;

            var leaveDay = HRMContext.Att_LeaveDay.Where(p => p.ID == id).FirstOrDefault();
            if (leaveDay != null)
            {
                leaveDay.IsDelete = true;
            }
            HRMContext.Att_LeaveDay.ApplyCurrentValues(leaveDay);
            int result = HRMContext.SaveChanges();
            return result != 0;
        }

        public void InsertLeaveDay(LeaveDayViewModel model)
        {
            //create new object Brand
            var leaveday = new Att_LeaveDay()
            {
                ID = Guid.NewGuid(),
                IsDelete = false,
                LeaveDayType = model.LeaveDayType,
                DateStart = model.DateStart,
                DateEnd = model.DateEnd,
                ProfileID = model.ProfileID,
                TotalDurian = model.TotalDurian,
                UserApproveID = model.UserApproveID,
                UserApproveID2 = model.UserApproveID2
            };
            HRMContext.Att_LeaveDay.AddObject(leaveday);
            //save change
            var result = HRMContext.SaveChanges();
        }

        public bool UpdateLeaveDay(LeaveDayViewModel model)
        {
            Guid id = model.ID;
            var leaveday = HRMContext.Att_LeaveDay.Where(p => p.ID == id).FirstOrDefault();
            leaveday.IsDelete = model.IsDelete;
            leaveday.LeaveDayType = model.LeaveDayType;
            leaveday.ProfileID = model.ProfileID;
            leaveday.TotalDurian = model.TotalDurian;
            leaveday.DateStart = model.DateStart;
            leaveday.DateEnd = model.DateEnd;
            leaveday.UserApproveID = model.UserApproveID;
            leaveday.UserApproveID2 = model.UserApproveID2;
            HRMContext.Att_LeaveDay.ApplyCurrentValues(leaveday);
            //save change
            var result = HRMContext.SaveChanges();
            return result != 0;
        }

        #endregion
    }
}
